#include "TIM4_Delay.h"


void TIM4_DelayInit(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);  ///使能TIM3时钟
	TIM_TimeBaseInitStructure.TIM_Period    = 50000-1; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1;     //定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//初始化TIM4
}

//微秒级延时
void TIM4_Delayus(uint16_t xus)
{
	TIM_Cmd(TIM4,ENABLE); //启动定时器
	while(TIM4->CNT < xus);
	TIM4->CNT = 0;
	TIM_Cmd(TIM4,DISABLE); //关闭定时器
}

//毫秒级延时
void TIM4_Delayms(uint16_t xms)
{
	int i;
	for(i=0;i<xms;i++)
	{
		TIM4_Delayus(1000);
	}
}


